<?php

namespace App\Http\Controllers\Admin;

use App\Admin as AdminModel;
use App\Http\Controllers\Controller;
use \Request;

class Admin extends Controller
{
    public function index()
    {

        $adminList = AdminModel::orderBy('id', 'desc')->paginate(15);
        $count     = AdminModel::count();
        return view('admin.admin.index', [
            'count'     => $count,
            'adminList' => $adminList,
        ]);
    }

    public function create()
    {
        return view('admin.admin.create');
    }

    public function store()
    {
        $result = AdminModel::create([
            'account'        => Request::input('account', 'test'),
            'login_password' => password_hash(Request::input('login_password', '123456'), PASSWORD_DEFAULT),
            'is_enable'      => Request::input('is_enable', 0),
        ]);
        return $result ? showSuccess('添加管理员成功！') : showError('添加管理员失败！');
    }

    public function edit($id = '')
    {
        $adminInfo = AdminModel::find($id);
        return view('admin.admin.edit', [
            'info' => $adminInfo,
        ]);
    }

    public function update($id = '')
    {
        $data = Request::all();
        if ($data['login_password']) {
            $adminData['login_password'] = password_hash($data['login_password'], PASSWORD_DEFAULT);
        }
        $adminData['account']   = Request::input('account');
        $adminData['is_enable'] = Request::input('is_enable', 0);

        $result = AdminModel::where('id', '=', $id)->update($adminData);
        return $result ? showSuccess('编辑管理员成功！') : showError('编辑管理员失败！');
    }

    public function destroy($id = '')
    {
        $result = AdminModel::where('id', '=', $id)->delete();
        return $result ? showSuccess('删除管理员成功！') : showError('删除管理员失败！');
    }
}
